package dao.server;

import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class TestSuite {
	private int id;
	private int idProject;
	private String name;
	private String time;
	private String description;
	private int code;
	private String checkOut;
	private ArrayList<TestCase> listTestCase;
	private ArrayList<TestResult> listTestResult;
	
	public TestSuite(){
		name = "";
		time = "";
		checkOut = "";
		description = "";
		listTestCase = new ArrayList<TestCase>();
		listTestResult = new ArrayList<TestResult>();
	}
	
	public TestSuite(int idProject, String name, String time,String description) {		
		this.idProject = idProject;
		this.name = name;
		this.time = time;
		this.checkOut = "";
		this.description = description;
		listTestCase = new ArrayList<TestCase>();
		listTestResult = new ArrayList<TestResult>();
	}
	
	public String getCheckOut(){
		return checkOut;
	}
	public void setCheckOut(String checkOut){
		this.checkOut = checkOut;
	}
	
	public int getCode(){
		return this.code;		
	}
	public void setCode(int code){
		this.code = code;
	}
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getIdProject() {
		return idProject;
	}
	public void setIdProject(int idProject) {
		this.idProject = idProject;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}

	public ArrayList<TestCase> getListTestCase() {
		return listTestCase;
	}

	public void setListTestCase(ArrayList<TestCase> listTestCase) {
		this.listTestCase = listTestCase;
	}

	public void getTestSuite(DAO dao){
		listTestCase = dao.getAllTestCaseInTestSuite(this.getId());
		listTestResult = dao.getAllTestResult(this.getId());
	}	
	
	public ArrayList<TestResult> getListTestResult() {		
		return listTestResult;
	}

	public void setListTestResult(ArrayList<TestResult> listTestResult) {
		this.listTestResult = listTestResult;
	}

	public StringWriter objectToXml(DocumentBuilder docBuilder) {
		try {
			// document elements
			Document doc = docBuilder.newDocument();
			Element document = doc.createElement("document");
			doc.appendChild(document);

			// set attribute to document element
			Attr type = doc.createAttribute("type");
			type.setValue("TESTSUITE");
			document.setAttributeNode(type);
			
			Attr action = doc.createAttribute("action");
			action.setValue("");
			document.setAttributeNode(action);

			// head elements
			Element head = doc.createElement("head");
			document.appendChild(head);

			// id elements
			Element id = doc.createElement("id");
			id.appendChild(doc.createTextNode(String.valueOf(this.getId())));
			head.appendChild(id);
			
			// id_project elements
			Element idProject = doc.createElement("id_project");
			idProject.appendChild(doc.createTextNode(String.valueOf(this
					.getIdProject())));
			head.appendChild(idProject);
			
			// name elements
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode(this.getName()));
			head.appendChild(name);

			// time elements
			Element time = doc.createElement("time");
			time.appendChild(doc.createTextNode(this.getTime()));
			head.appendChild(time);
			
			// description elements
			Element description = doc.createElement("description");
			description.appendChild(doc.createTextNode(this.getDescription()));
			head.appendChild(description);
			
			// code elements
			Element code = doc.createElement("code");
			code.appendChild(doc.createTextNode(String.valueOf(this.getCode())));
			head.appendChild(code);
			
			// check out elements
			Element checkOut = doc.createElement("checkout");
			checkOut.appendChild(doc.createTextNode(this.getCheckOut()));
			head.appendChild(checkOut);
			
			// body elements
			Element body = doc.createElement("body");			
			document.appendChild(body);
			
			// suite elements
			Element suite = doc.createElement("suite");
			body.appendChild(suite);

			if (listTestCase != null && listTestCase.size() != 0) {
				for (TestCase tc : listTestCase) {
					// test case elements
					Element testcase = doc.createElement("testcase");
					testcase.appendChild(doc.createTextNode(tc.getName()));
					suite.appendChild(testcase);

					// set attribute to test case element
					Attr idTC = doc.createAttribute("id");
					idTC.setValue(String.valueOf(tc.getId()));
					testcase.setAttributeNode(idTC);
					
					// set attribute to test case element
					Attr order = doc.createAttribute("order");
					order.setValue(String.valueOf(tc.getOrder()));
					testcase.setAttributeNode(order);
				}
			}
			
			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			transformer.transform(source, result);
			return sw;
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}
	public void xmlToObject(Document doc){
		try {
			NodeList nodeList = doc.getElementsByTagName("head");
			Element element = (Element)nodeList.item(0);
			try{
				this.setId(Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setId(0);
			}
			try{
				this.setIdProject(Integer.parseInt(element.getElementsByTagName("id_project").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setIdProject(0);
			}
			this.setName(element.getElementsByTagName("name").item(0).getTextContent());
			this.setTime(element.getElementsByTagName("time").item(0).getTextContent());
			this.setDescription(element.getElementsByTagName("description").item(0).getTextContent());
			try{
				this.setCode(Integer.parseInt(element.getElementsByTagName("code").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setCode(0);
			}
			this.setCheckOut(element.getElementsByTagName("checkout").item(0).getTextContent());
			NodeList testcaseList  = doc.getElementsByTagName("testcase");
			for(int i = 0; i < testcaseList.getLength(); i++){
				Element testcase = (Element)testcaseList.item(i);
				TestCase tc = new TestCase();
				try{
					tc.setId(Integer.parseInt(testcase.getAttribute("id")));
				}catch(NumberFormatException nfe){
					tc.setId(0);
				}
				try{
					tc.setOrder(Integer.parseInt(testcase.getAttribute("order")));
				}catch(NumberFormatException nfe){
					tc.setOrder(-1);
				}
				tc.setName(testcase.getTextContent());
				this.listTestCase.add(tc);
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}
	
	public String addTestSuite(DAO dao){
		if(dao != null)
			if(dao.addTestSuite(this))
				return String.valueOf(id);
			else
				return "Create test suite " + this.getName() + " unsuccess, please try again!";
		return "Create test suite " + this.getName() + " unsuccess, please try again!";
	}
	public String updateTestSuite(DAO dao){
		if(dao != null){
			TestSuite ts = dao.getTestSuite(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test suite";
			if(!this.getCheckOut().equals(ts.getCheckOut()))
				return "You must check out test suite " + this.getName() + " before you want to save it";
			if(dao.updateTestSuite(this))
				if(dao.updateTestCaseInTestSuite(this))
					return "Update test suite success";
				else
					return "Not complete! Some test case not found. Please refresh and try again";			
		}
		return "Update test suite unsuccess, please try again!";
	}
	public String renameTestSuite(DAO dao){
		if(dao != null){
			TestSuite ts = dao.getTestSuite(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test suite";
			if(dao.renameTestSuite(this))
				return "Rename test suite success";						
		}
		return "Rename test suite unsuccess, please try again!";
	}
	public String deleteTestSuite(DAO dao){
		if(dao != null){
			TestSuite ts = dao.getTestSuite(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test suite";
			if(dao.deleteTestSuite(ts))
				return "Remove test suite " + this.getName() + " success";
		}
		return "Remove test suite " + this.getName() + " unsuccess, please try again!";
	}
	public String checkOutTestSuite(DAO dao){
		if(dao != null){
			TestSuite ts = dao.getTestSuite(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test suite " + this.getName();
			else
			{
				if(!ts.getCheckOut().equals("") && !ts.getCheckOut().equals(this.getCheckOut()))
					return "This test suite is checked out. Please, try again later!";
				if(ts.getCode() != this.getCode())
					return "This test suite is old version. Please, refreshing it and try again!";
				if(dao.checkOutTestSuite(this))
					return "Check out success";
				else
					return "Check out test suite " + this.getName() + " unsuccess, please try again!";
			}
		}
		return "Check out test suite " + this.getName() + " unsuccess, please try again!";	
	}
	public String releaseCheckOutTestSuite(DAO dao){
		if(dao != null){
			this.setCheckOut("");
			TestSuite ts = dao.getTestSuite(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test suite";
			if (dao.checkOutTestSuite(this))
				return "Release check out success";
		}
		return "Release check out test suite " + this.getName() + " unsuccess, please try again!";	
	}
	public String checkInTestSuite(DAO dao){
		if(dao != null){
			this.setCheckOut("");
			TestSuite ts = dao.getTestSuite(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test suite";
			if (dao.checkOutTestSuite(this))
				return "Check in success";
		}
		return "Check in test suite " + this.getName() + " unsuccess, please try again!";	
	}
}
